package com.nancal.activiti.controller;

import com.github.pagehelper.Page;
import com.nancal.activiti.entity.Response;
import com.nancal.activiti.entity.query.BasePageQuery;
import com.nancal.activiti.service.process.ProcessRuntimeService;
import com.nancal.activiti.service.process.ProcessTaskService;
import com.nancal.common.core.controller.BaseController;
import com.nancal.common.core.page.TableDataInfo;
import org.activiti.engine.task.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 流程操作
 */
@RestController
@RequestMapping("workflow/processRuntime")
public class ProcessRuntimeController extends BaseController {

    @Autowired
    ProcessRuntimeService processRuntimeService;
    @Autowired
    ProcessTaskService processTaskService;

    /**
     * 根据流程定义key启动流程实例
     * 由于前端画的的流程图的processDefinitionKey是固定的Process_1，暂无法使用该方法
     * http://localhost:8080/workflow/processRuntime/startProcessInstanceById?definitionKey=Process_1&username=username
     */
    @RequestMapping("/startProcessInstance")
    public void startProcessInstance(String definitionKey, String username) {
        processRuntimeService.startProcessInstanceByKey(definitionKey, username);
    }

    /**
     * 通过流程定义id启动流程
     * http://localhost:8080/workflow/processRuntime/startProcessInstanceById?deploymentId=deploymentId&username=username
     */
    @RequestMapping("/startProcessInstanceById")
    public Response startProcessInstanceById(String deploymentId, String username) {

        Map<String, String> response = processRuntimeService.startProcessInstanceByDefId(deploymentId, username);
        return Response.success(response);
    }

    /**
     * 给流程实例添加附件
     * post
     * http://localhost:8080/workflow/processRuntime/saveAttachment
     */
    @RequestMapping("/saveAttachment")
    public Response saveAttachment(MultipartFile file, String taskId, String processInstanceId) {
        if (Objects.isNull(file)) {
            return Response.failure("文件上传失败，请重新选择文件");
        }
        processRuntimeService.saveAttachment(file, taskId, processInstanceId);
        return Response.success();
    }

    /**
     * 查看流程实例对应的附件
     * get
     * http://localhost:8080/workflow/processRuntime/getAttachment?processInstanceId=processInstanceId
     */
    @RequestMapping("/getAttachment")
    public String getAttachment(String processInstanceId, HttpServletResponse response) {

        return processRuntimeService.getAttachment(processInstanceId, response);

    }


    /**
     * 删除流程实例(申请撤销)
     * runtimeService.deleteProcessInstance(processInstanceId,"删除原因");//删除流程
     * http://localhost:8080/workflow/processRuntime/deleteProcessInstanceById?processInstanceId=200008&message=申请原因写错
     */
    @RequestMapping("/deleteProcessInstanceById")
    public void deleteProcessInstanceById(String processInstanceId, String message) {
        processRuntimeService.deleteProcessInstanceById(processInstanceId, message);
    }

    /**
     * 完成任务，并设置审批意见
     * http://localhost:8080/workflow/processRuntime/completeTask?taskId=115010&username=李四&message=同意
     *
     * @param taskId
     * @param message
     */
    @GetMapping("completeTask")
    public Response completeTask(String taskId, String username, String message) {
        processTaskService.completeTask(taskId, username, message);
        return Response.success();
    }

    /**
     * 根据任务id查询审批意见
     * http://localhost:8080/workflow/processRuntime/getComment?taskId=115010
     */
    @GetMapping("getComment")
    public Response getComment(String taskId) {
        List<Comment> comment = processTaskService.getComment(taskId);
        return Response.success(comment);
    }

    /**
     * 获取历史评论信息
     * http://localhost:8080/workflow/processRuntime/getComments?processInstanceId=115005
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping("getComments")
    public Response getComments(String processInstanceId) {
        List<Map<String, Object>> comments = processTaskService.getComments(processInstanceId);
        return Response.success(comments);
    }

    /**
     * 指派用户任务
     * http://localhost:8080/workflow/processRuntime/assigneeTask?taskId=115010&username=李四
     *
     * @param taskId
     * @param username
     */
    @GetMapping("assigneeTask")
    public Response assigneeTask(String taskId, String username) {
        processTaskService.assigneeTask(taskId, username);
        return Response.success();
    }

    /**
     * 查询下一个节点的连线信息
     */
    @RequestMapping("findOutComeListByTaskId")
    public Response findOutComeListByTaskId(String taskId) {
        processTaskService.findOutComeListByTaskId(taskId);
        return Response.success();
    }

    /**
     * 审批流程
     * http://localhost:8080/workflow/processRuntime/handleProcess
     *
     * @param map
     * @return
     */
    @RequestMapping("handleProcess")
    public Response handleProcess(@RequestBody Map<String, String> map) {
        String status = map.get("status");
        String taskId = map.get("taskId");
        String username = map.get("username");
        String message = map.get("message");
        if (status.equals("同意")) {
            agreeProcess(taskId, username);
        }
        if (status.equals("驳回")) {
            disAgreeProcess(taskId, username, message);
        }

        return Response.success();
    }

    /**
     * 同意审批
     * http://localhost:8080/workflow/processRuntime/agree?taskId=195014&username=赵六
     *
     * @param taskId
     * @return
     */
    @GetMapping("agree")
    public Response agreeProcess(String taskId, String username) {
        processTaskService.agreeProcess(taskId, username);
        return Response.success();
    }


    /**
     * 驳回审批
     * http://localhost:8080/workflow/processRuntime/disAgree?taskId=190003&username=赵六&message=1
     *
     * @param taskId
     * @return
     */
    @GetMapping("disAgree")
    public Response disAgreeProcess(String taskId, String username, String message) {
        processTaskService.disAgreeProcess(taskId, username, message);
        return Response.success();
    }

    /**
     * 获取用户需要处理的Task
     * 未处理流程
     * http://localhost:8080/workflow/processRuntime/getUserTask?username=张三
     *
     * @param pageQuery
     * @param username
     * @param request
     * @return
     */
    @PostMapping("getUserTask")
    public Response getUserTask(@RequestBody BasePageQuery pageQuery, String username, HttpServletRequest request) {
        //从session中获取到用户名
        //String currentUserName = SessionUtils.getCurrentUserName(request);
        Page<List<Map<String, String>>> userTasksPage = processTaskService.getUserTasks(pageQuery, username);
        TableDataInfo dataTable = getDataTable(userTasksPage);
        return Response.success(dataTable);
    }

    /**
     * 我的流程（发起人看到自己得申请信息）
     * http://localhost:8080/workflow/processRuntime/myProcess?organizerName=张三
     */
    @PostMapping("myProcess")
    public Response myProcess(String organizerName, @RequestBody BasePageQuery pageQuery) {
        Page<List<Map<String, String>>> myProcess = processTaskService.findMyProcess(organizerName, pageQuery);
        TableDataInfo dataTable = getDataTable(myProcess);
        return Response.success(dataTable);
    }

    /**
     * 已处理流程
     * http://localhost:8080/workflow/processRuntime/myHisProcess?username=张三
     */
    @PostMapping("myHisProcess")
    public Response myHisProcess(String username, @RequestBody BasePageQuery pageQuery) {
        Page<List<Map<String, String>>> myProcess = processTaskService.findMyHisProcess(username, pageQuery);
        TableDataInfo dataTable = getDataTable(myProcess);
        return Response.success(dataTable);
    }

}
